package com.enpronomics.pcall.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.enpronomics.pcall.R;
import com.enpronomics.pcall.ServiceManager;

public class PanicSettingsActivity extends Activity implements OnClickListener {
	private Button contact1Btn, contact2Btn, contact3Btn, contact4Btn,
			contact5Btn;
	private Button selectedView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.panic_contacts_layout);

		contact1Btn = (Button) findViewById(R.id.panic_contact1_button);
		contact2Btn = (Button) findViewById(R.id.panic_contact2_button);
		contact3Btn = (Button) findViewById(R.id.panic_contact3_button);
		contact4Btn = (Button) findViewById(R.id.panic_contact4_button);
		contact5Btn = (Button) findViewById(R.id.panic_contact5_button);

		contact1Btn.setOnClickListener(this);
		contact2Btn.setOnClickListener(this);
		contact3Btn.setOnClickListener(this);
		contact4Btn.setOnClickListener(this);
		contact5Btn.setOnClickListener(this);

		contact1Btn.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_CONTACT1));
		contact2Btn.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_CONTACT2));
		contact3Btn.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_CONTACT3));
		contact4Btn.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_CONTACT4));
		contact5Btn.setText(ServiceManager.getStringSetting(this,
				ServiceManager.KEY_CONTACT5));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		selectedView = (Button) v;
		startActivityForResult(new Intent(Intent.ACTION_PICK,
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI), 0);
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);
		String buttonKey = null;
		if (selectedView == contact1Btn) {
			buttonKey = ServiceManager.KEY_CONTACT1;
		} else if (selectedView == contact2Btn) {
			buttonKey = ServiceManager.KEY_CONTACT2;
		} else if (selectedView == contact3Btn) {
			buttonKey = ServiceManager.KEY_CONTACT3;
		} else if (selectedView == contact4Btn) {
			buttonKey = ServiceManager.KEY_CONTACT4;
		} else if (selectedView == contact5Btn) {
			buttonKey = ServiceManager.KEY_CONTACT5;
		}
		try {
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					String telNumber = c
							.getString(c
									.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
					telNumber = telNumber.replace("-", "");
					telNumber = telNumber.replace("+2", "");
					ServiceManager.setStringSetting(this, buttonKey, telNumber);
					selectedView.setText(telNumber);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
